<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>

        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="渠道管理">

            <div style="text-align: right; margin-bottom: 20px">
              <el-button type="primary" size="small" @click="clickAdd(1, '0')">新增</el-button>
            </div>
            <el-table row-key="id" :data="list" border>
              <el-table-column label="序号" prop="name" width="50">
                <template #default="{ row }">
                  <!-- <i class="el-icon-receiving" /> -->
                  {{ row.id }}
                </template>
              </el-table-column>
              <el-table-column label="渠道名称" prop="name" width="120" />
              <el-table-column label="开始日期" prop="startDate" width="160"/>
              <el-table-column label="结束日期" prop="endDate" width="160"/>
              <el-table-column label="状态" prop="state" />
              <el-table-column label="是否含增值税" prop="IfAdditional" width="120" />
              <el-table-column label="申报金额" prop="declaredAmount" />
              <el-table-column label="收税比例（整数部分）" prop="taxInteger" width="180" />
              <el-table-column label="收税比例（小数部分）" prop="taxDecimal" width="180" />
              <el-table-column label="货物性质" prop="natureOfGoods" />
              <el-table-column label="体积重收费策略" prop="volumeWeightType" width="150 " />
              <el-table-column label="首重" prop="firstWeight" />
              <el-table-column label="首重价格" prop="firstPrice" />
              <el-table-column label="续重" prop="nextWeight" />
              <el-table-column label="续重价格" prop="nextPrice" />
              <el-table-column label="首重成本价格" prop="costFirstPrice" width="150" />
              <el-table-column label="续重成本价格" prop="costNextPrice" width="150" />
              <el-table-column prop="text" fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                  <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <span>确定删除吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteComfirm">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form v-loading="loading" label-width="150px">
          <el-form-item label="渠道名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="开始日期" prop="startDate">
            <el-input v-model="formData.startDate" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="结束日期" prop="endDate">
            <el-input v-model="formData.endDate" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="状态" prop="state">
            <el-input v-model="formData.state" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="是否含增值税" prop="ifAdditional">
            <el-input v-model="formData.ifAdditional" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="申报金额" prop="declaredAmount">
            <el-input v-model="formData.declaredAmount" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="收税比例（整数部分）" prop="taxInteger">
            <el-input v-model="formData.taxInteger" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="收税比例（小数部分）" prop="taxDecimal">
            <el-input v-model="formData.taxDecimal" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="货物性质" prop="natureOfGoods">
            <el-input v-model="formData.natureOfGoods" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="运输模式" prop="modeOfTransport">
            <el-input v-model="formData.modeOfTransport" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="体积重收费策略" prop="volumeWeightType">
            <el-input v-model="formData.volumeWeightType" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="首重" prop="firstWeight">
            <el-input v-model="formData.firstWeight" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="首重价格" prop="firstPrice">
            <el-input v-model="formData.firstPrice" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="续重" prop="nextWeight">
            <el-input v-model="formData.nextWeight" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="续重价格" prop="nextPrice">
            <el-input v-model="formData.nextPrice" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="首重成本价格" prop="costFirstPrice">
            <el-input v-model="formData.costFirstPrice" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="续重成本价格" prop="costNextPrice">
            <el-input v-model="formData.costNextPrice" placeholder="请输入" />
          </el-form-item>

        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确认</el-button>
          </div>
        </template>
      </el-dialog>

    </div>

    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="list.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>

import { selectChannel, insertChannel, updateChannel, deleteChannel } from '@/api/quotationSystem'
export default {

  data() {
    return {
      dialogVisible: false,
      // 分页数据
      currentPage: 1,
      list: [],
      pageParam: { // 分页参数对象
        page: '',
        num: ''
      },
      showDialog: false,
      formData: {
        name: '', // 名称
        startDate: '',
        endDate: '',
        state: '',
        ifAdditional: '',
        declaredAmount: '',
        taxInteger: '',
        taxDecimal: '',
        natureOfGoods: '',
        modeOfTransport: '',
        volumeWeightType: '',
        firstWeight: '',
        firstPrice: '',
        nextWeight: '',
        nextPrice: '',
        costFirstPrice: '',
        costNextPrice: ''

      },
      loading: false,
      permissionData: [] // 存放用于展示的权限列表数据 (树形结构)
    }
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return '编辑'
      } else {
        return '添加'
      }
    }
  },
  mounted() {
    console.log('进入页面的钩子函数')
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const data = await selectChannel(this.pageParam)
      console.log('请求响应结果', data)
      this.list = data.list
      this.dataTotal = data.total
    },
    clickAdd(type, pid) {
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      // 重置清空数据
      this.formData = {
        name: '', // 名称
        startDate: '',
        endDate: '',
        state: '',
        ifAdditional: '',
        declaredAmount: '',
        taxInteger: '',
        taxDecimal: '',
        natureOfGoods: '',
        modeOfTransport: '',
        volumeWeightType: '',
        firstWeight: '',
        firstPrice: '',
        nextWeight: '',
        nextPrice: '',
        costFirstPrice: '',
        costNextPrice: ''
      }
  
    },
    // 点击弹框的确认按钮
    async clickSubmit() {
      if (this.formData.name === '') {
        this.$message.error('渠道名称不能为空')
        return
      }
      if (this.formData.id) {
        // 编辑操作
        const { postalcode, ...formData } = this.formData
        await updateChannel(formData)
      } else {
        // 添加操作
        await insertChannel(this.formData)
      }
      this.showDialog = false
      this.$message.success('操作成功')
      this.getPermissionList()
    },
    // 删除功能
    handleDelete(id) {
      this.deleteId = id
      this.dialogVisible = true
    },
    async deleteComfirm() {
      this.dialogVisible = false
      await deleteChannel({
        id: this.deleteId
      })
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getPermissionList()
    },
    handleEdit(row) {
      console.log(row)
      // const {postalCode,...formData} = row
      // formData.postalcode = postalCode
      this.formData = row
      this.formData.postalcode = row.postalCode
      this.showDialog = true
    },
    // 点击查看编辑按钮, 显示弹框
    clickEdit(id) {
      // 显示弹框
      this.showDialog = true
      this.loading = true
      // 发送请求, 获取数据, 进行回显
      // const { data } = await reqGetPermissionDetail(id)
      // this.formData = data

      this.loading = false
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style>

</style>
